package com.atguigu.gmall.cart.controller;

import com.atguigu.gmall.cart.service.CartInfoService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.model.cart.CartInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api("购物车")
@RestController
@RequestMapping("/api/cart")
public class CartApiController {

    @Autowired
    private CartInfoService cartInfoService;

    @ApiOperation("添加购物车")
    @PostMapping("/addCart/{skuId}/{skuNum}")
    public void addCart(@PathVariable Long skuId, @PathVariable Integer skuNum) {
        cartInfoService.addCart(skuId, skuNum);
    }

    @ApiOperation("获取购物车中间信息")
    @PostMapping("/toCart/{skuId}")
    public CartInfo toCart(@PathVariable Long skuId) {
        CartInfo cartInfo = cartInfoService.toCart(skuId);
        return cartInfo;
    }

    @ApiOperation("整合购物车")
    @GetMapping("/cartList")
    public Result cartList(HttpServletRequest request) {
        String userId = request.getHeader("userId");
        String userTempId = request.getHeader("userTempId");
        List<CartInfo> list = cartInfoService.cartList(userId, userTempId);
        return Result.ok(list);
    }

    @ApiOperation("更新购物项状态")
    @GetMapping("/checkCart/{skuId}/{isChecked}")
    public Result checkCart(@PathVariable Long skuId, @PathVariable Integer isChecked) {
        cartInfoService.checkCart(skuId, isChecked);
        return Result.ok();
    }

    @ApiOperation("远程调用商品清单")
    @GetMapping("/getCartListChecked")
    public List<CartInfo> getCartListChecked(HttpServletRequest request) {
        String userId = AuthContextHolder.getUserId(request);
        return cartInfoService.getCartListChecked(userId);

    }
}
